<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Management extends CCMIS_Controller{

    public function __construct() {
        parent::__construct();
        $this->load->model('rmis/mdl_rmis','research');
        ///$this->load->model('mdl_faculty');
    }

    public function index(){
        $data = array (
            'page_title' => 'RMIS | Management'
            );
        $this->setup_view('rmis/management/index',$data);
    }

    public function research(){
        $data = array(
            'page_title' => 'RMIS | Research Management'
        );
        $this->setup_view('rmis/management/research',$data);
    }

    public function researchAction(){
        $data = array(
           'page_title' => 'RMIS | Research Management'
        );

        if(isset($_POST['go'])){
            switch($_POST['action']){
                case 1:
                    $data['stat'] = "validate upload";
                    $RData = $this->research->getTempResearch();
                    if(sizeof($RData) > 0 ){
                        $data['res'] = $RData;
                        $i = 0;
                        $Author[] = null;
                        foreach ($RData as $res) {
                            $Author[$i] = $this->research->getResearchAuthor($res['RID']);
                            $i++;
                        }
                        $data['auth'] = $Author;
                    } else {
                        $data['msg'] = "None";
                    }

                    $this->setup_view('rmis/management/researches', $data);
                    
                break;
                case 2:
                    $data['stat'] = "validate modified";
                    $RData = $this->research->getModResearch();
                    if(sizeof($RData) > 0 ){
                        $data['res'] = $RData;
                        $i = 0;
                        $Author[] = null;
                        foreach ($RData as $res) {
                            $Author[$i] = $this->research->getResearchModAuthor($res['RID']);
                            $i++;
                        }
                        $data['auth'] = $Author;
                    } else {
                        $data['msg'] = "None";
                    }
                    
                    $this->setup_view('rmis/management/researches', $data);
                break;
                case 3:
                    $i = 0;
                    $data['stat'] = "grant view";
                    $view = $this->research->getMemberViewTemp();
                    
                    if($view[0] > 0){
                        $data['view'] = $view;
                        //print_r($data);
                    } else {
                        $data['msg'] = "None";
                    }

                    $this->setup_view('rmis/management/view_researches', $data);
                break;
                case 4:
                break;
            }
        }
    }

    public function researchesAction(){
        $data = array(
           'page_title' => 'RMIS | Research Management'
        );

        if(isset($_POST['AUploaded'])){
            $temp = $this->research->getResearchbyRESID($_POST['AUploaded']);
            if(sizeof($temp) > 0){
                $this->research->insertresearch($temp);
                $var = $this->research->getResearchAuthorTemp($_POST['AUploaded']);
                $this->research->insertAuthor($var);
                $this->research->deleteaccount_research_temp($_POST['AUploaded']);
                $this->research->deleteresearch_temp($_POST['AUploaded']);
                
                $data['stat'] = "validate upload";
                $RData = $this->research->getTempResearch();
                if(sizeof($RData) > 0 ){
                    $data['res'] = $RData;
                    $i = 0;
                    $Author[] = null;
                    foreach ($RData as $res) {
                        $Author[$i] = $this->research->getResearchAuthor($res['RID']);
                        $i++;
                    }
                    $data['auth'] = $Author;
                } else {
                    $data['msg'] = "None";
                }

                $this->setup_view('rmis/management/researches', $data);
            }
            
        }

        if(isset($_POST['RUploaded'])){
            
            $this->research->deleteaccount_research_temp($_POST['RUploaded']);
            $this->research->deleteresearch_temp($_POST['RUploaded']);
            
            $data['stat'] = "validate upload";
            $RData = $this->research->getTempResearch();
            if(sizeof($RData) > 0 ){
                $data['res'] = $RData;
                $i = 0;
                $Author[] = null;
                foreach ($RData as $res) {
                    $Author[$i] = $this->research->getResearchAuthor($res['RID']);
                    $i++;
                }
                $data['auth'] = $Author;
            } else {
                $data['msg'] = "None";
            }

            $this->setup_view('rmis/management/researches', $data);
        }

        if(isset($_POST['AModified'])){

            $this->research->updateResearchType($_POST['AModified'],0);

            $data['stat'] = "validate modified";
            $RData = $this->research->getModResearch();
            if(sizeof($RData) > 0 ){
                $data['res'] = $RData;
                $i = 0;
                $Author[] = null;
                foreach ($RData as $res) {
                    $Author[$i] = $this->research->getResearchModAuthor($res['RID']);
                    $i++;
                }
                $data['auth'] = $Author;
            } else {
                $data['msg'] = "None";
            }
            
            $this->setup_view('rmis/management/researches', $data);
        }

        if(isset($_POST['AGrant'])){
            $view = explode("*",$_POST['AGrant']);
            $this->research->insertresearchview($view);
            $this->research->deleteviewtemp($view);

            $i = 0;
            $data['stat'] = "grant view";
            $view = $this->research->getMemberViewTemp();
            
            if(sizeof($view)>0){
                $data['view'] = $view;
                //print_r($data);
            } else {
                $data['msg'] = "None";
            }

            $this->setup_view('rmis/management/view_researches', $data);
        }

        if(isset($_POST['RGrant'])){
            $view = explode("*",$_POST['RGrant']);
            
            $this->research->deleteviewtemp($view);

            $i = 0;
            $data['stat'] = "grant view";
            $view = $this->research->getMemberViewTemp();
            
            if(sizeof($view)>0){
                $data['view'] = $view;
            } else {
                $data['msg'] = "None";
            }

            $this->setup_view('rmis/management/view_researches', $data);
        }

    }

    public function account(){
		$data = array(
            'page_title' => 'RMIS | Account Management'
        );
        $this->setup_view('rmis/management/account',$data);
	}
    
    public function accountAction(){
        $data = array(
           'page_title' => 'RMIS | Account Management'
        );
        if(isset($_POST['go'])){
            switch($_POST['action']){
                case 1:
                    $data = $this->populate(2);
                    $this->setup_view('rmis/management/grantAccount', $data);
                break;
                case 2:
                    $data = $this->populate(1);
                    $this->setup_view('rmis/management/terminateAccount', $data);
                break;
                case 3:
                    $data = $this->populate(0);
                    $this->setup_view('rmis/management/regainAccount', $data);
                break;
            }
        }
    }

    public function grantAction(){
        $data = array(
           'page_title' => 'RMIS | Account Management'
        );
        if(isset($_POST['A'])){
            if($this->research->updateMemberStatus($_POST['A'], 1) == 1){
                $msg = "Account granted.";
            }
        } else if (isset($_POST['R'])) {
            if($this->research->updateMemberStatus($_POST['R'], 0) == 1){
                $msg = "Account rejected.";
            }                
        } else if(isset($_POST['Next'])) {

        }

        $data = $this->populate(1);
        $data['msg'] = $msg;
        $this->setup_view('rmis/management/grantAccount', $data);

        //echo $msg;
    }

    public function terminateAction(){
        $data = array(
           'page_title' => 'RMIS | Account Management'
        );
        if(isset($_POST['A'])){
            if($this->research->updateMemberStatus($_POST['A'], 0) == 1){
                $msg = "Account terminated.";
            }
        } else if(isset($_POST['Next'])) {

        }

        $data = $this->populate(1);
        $data['msg'] = $msg;
        $this->setup_view('rmis/management/terminateAccount', $data);

        //echo $msg;
    }

    public function regainAction(){
        $data = array(
           'page_title' => 'RMIS | Account Management'
        );
        if(isset($_POST['A'])){
            if($this->research->updateMemberStatus($_POST['A'], 1) == 1){
                $msg = "Account regained.";
            }
        } else if(isset($_POST['Next'])) {

        }

        $data = $this->populate(1);
        $data['msg'] = $msg;
        $this->setup_view('rmis/management/regainAccount', $data);

        //echo $msg;
    }

    private function populate($ID){
        $i = 0;
        $MemID = $this->research->getMemberID($ID);
        if(sizeof($MemID) > 0 && $MemID[0]['ID'] > 0){
            $MemDet[] = null;
            $MemDept[] = null;
            foreach ($MemID as $ID) {
                $MemDet[$i] = $this->research->getMemberDetails($ID['ID']);
                $MemDept[$i] = $this->research->getMemberDept($ID['ID']);
                $i++;
            }
            //print_r ($MemDept);
            //print_r ($MemDet);
            $data['det'] = $MemDet;
            $data['dep'] = $MemDept;
            $data['mem'] = $MemID;
            $data['count'] = sizeof($MemDet);
        } else {
            $data['msg'] = "None.";
        }
        return $data;
    }
}
?>